﻿using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading;
using DMS.Business.Utility;

namespace DMS.Web.Common.Services
{
    public partial class GetRandomImage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //生成随机码图片
            SetValidateCode();
            //生成页面不保存到cache
            Response.Cache.SetNoStore();
        }

        //设置验证码
        private void SetValidateCode()
        {
            //新建位图
            Bitmap newBitmap = new Bitmap(
                                            71,
                                            27,
                                            PixelFormat.Format32bppArgb
                                         );
            //从位图获得绘图画面
            Graphics g = Graphics.FromImage(newBitmap);
            //随机数生成器
            Random r = new Random();
            //绘图画面清空
            g.Clear(Color.White);
            //绘图画面划线干扰
            for (int i = 0; i < 50; i++)
            {
                int x1 = r.Next(newBitmap.Width);
                int x2 = r.Next(newBitmap.Width);
                int y1 = r.Next(newBitmap.Height);
                int y2 = r.Next(newBitmap.Height);
                g.DrawLine(new Pen(
                                    Color.FromArgb(r.Next())),
                                    x1,
                                    y1,
                                    x2,
                                    y2
                                  );
            }
            //绘图画面点数干扰
            for (int i = 0; i < 100; i++)
            {
                int x = r.Next(newBitmap.Width);
                int y = r.Next(newBitmap.Height);
                newBitmap.SetPixel(
                                    x,
                                    y,
                                    Color.FromArgb(r.Next())
                                  );
            }
            //获得随机字符串（5位长度）
            string value = GenerateRandom(5);
            //随机字符串赋值给Session
            Session["RandCode"] = value;
            //定义图片显示字体样式
            Font font = new Font(
                                   "Arial",
                                   14,
                                   FontStyle.Bold
                                );
            Random rr = new Random();
            int yy = rr.Next(1, 4);
            //定义随机字符串显示图片刷子
            LinearGradientBrush brush = new LinearGradientBrush(
                                                                  new Rectangle(0, 0, 71, 23),
                                                                  Color.Red,
                                                                  Color.Blue,
                                                                  1.2f,
                                                                  true
                                                               );
            g.DrawString(value, font, brush, 2, yy);
            g.DrawRectangle(new Pen(
                                      Color.Silver),
                                      0,
                                      0,
                                      70,
                                      22
                                    );
            MemoryStream ms = new MemoryStream();
            newBitmap.Save(ms, ImageFormat.Gif);
            //输出图片
            try
            {
                Response.ClearContent();
                Response.ContentType = "image/gif";
                Response.BinaryWrite(ms.ToArray());
                HttpContext.Current.ApplicationInstance.CompleteRequest();
            }
            catch (ThreadAbortException ex)
            {
                // Nothing 这个是正常的，.net Framework的问题
                // http://www.cnblogs.com/zjoch/archive/2008/12/02/1345485.html
            }
            catch (Exception ex)
            {
                LogHelper.LogException2(ex);
            }
        }

        //常量集
        private static char[] constant ={
                                        '0','1','2','3','4','5','6','7','8','9',
                                        'a','b','c','d','e','f','g','h','i','j',
                                        'k','l','m','n','o','p','q','r','s','t',
                                        'u','v','w','x','y','z'
                                    };


        //生成随机字符串
        public static string GenerateRandom(int Length)
        {
            System.Text.StringBuilder newRandom = new System.Text.StringBuilder(36);
            Random rd = new Random();
            for (int i = 0; i < Length; i++)
            {
                newRandom.Append(constant[rd.Next(36)]);
            }
            return newRandom.ToString();
        }


    }
}